using PP.Basic;
using PP.PPPoker.Game.GJ25;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace PP.PPPoker.Game.GJ25
{
    /// <summary>
    /// GJCardGroup 对象
    /// </summary>
    public class GJCardGroup : MonoBehaviour
    {
        private const string Tag = nameof(GJCardGroup);

        // Private Variables Begin
        private List<GJCard> _cardList = new List<GJCard>();
        // Private Variables End

        // Static Methods Begin
        public static GJCardGroup Create()
        {
            return null;
        }
        // Static Methods End
        
        // Public Methods Begin

        public void Deal(bool needAnim = false)
        {
            for (int i = 0; i < _cardList.Count; i++)
            {
                var card = _cardList[i];
                if (card.Code == 0)
                {
                    var nextCode = CardDealer.Instance.GetNextCode();
                    if (nextCode > 0)
                    {
                        card.SetCardCode(nextCode);
                        if (needAnim)
                            card.PlayDealIn();
                    }
                    else
                    {
                        Debug.LogWarning($"{Tag}.Deal -> No more cards to deal. XXXXXXXXXXXXXXXXXXXXXXXX");
                    }
                }
            }
        }


        public int GetLeftCard()
        {
            var leftCardCount = 0;
            for (int i = 0; i < _cardList.Count; i++)
            {
                var card = _cardList[i];
                if (card.Code > 0)
                {
                    leftCardCount++;
                }
            }
            return leftCardCount;
        }



        public List<GJCard> GetSelectedCard()
        {
            List<GJCard> selectedCards = new List<GJCard>();
            foreach (var card in _cardList)
            {
                if (card.IsSelected)
                {
                    selectedCards.Add(card);
                }
            }
            return selectedCards;
        }


        public void ClearSelectedCard()
        {
            foreach (var card in _cardList)
            {
                if (card.IsSelected)
                {
                    card.PlayDown();
                }
            }
        }

        // Public Methods End
        
        // Lifecycle Methods Begin
        protected void Awake()
        {
            InitHier();
            InitPart();
        }

        private void InitPart()
        {
            _cardList.Add(_1GJCard);
            _cardList.Add(_2GJCard);
            _cardList.Add(_3GJCard);
            _cardList.Add(_4GJCard);
            _cardList.Add(_5GJCard);

        }
        
        private void Start()
        {
            Debug.Log($"{Tag}.Start");
        }
 
        // Lifecycle Methods End

        // Hier Components Begin


        // Hier Components Begin        
        private GJCard _1GJCard;
        private GJCard _2GJCard;
        private GJCard _3GJCard;
        private GJCard _4GJCard;
        private GJCard _5GJCard;

        private void InitHier()
        {
            _1GJCard = transform.Find("_GJCard_1#").GetComponentEx<GJCard>();
            //_1GJCard.Initiate();
            _2GJCard = transform.Find("_GJCard_2#").GetComponentEx<GJCard>();
            //_2GJCard.Initiate();
            _3GJCard = transform.Find("_GJCard_3#").GetComponentEx<GJCard>();
            //_3GJCard.Initiate();
            _4GJCard = transform.Find("_GJCard_4#").GetComponentEx<GJCard>();
            //_4GJCard.Initiate();
            _5GJCard = transform.Find("_GJCard_5#").GetComponentEx<GJCard>();
            //_5GJCard.Initiate();
        }

        // Hier Components End
    }
}
